package com.bona.dubbo.system.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.bona.cache.SysMultLangCache;
import com.bona.constants.CacheConstant;
import com.bona.dubbo.system.ISysCommonDubboSV;
import com.bona.model.system.SysCategoryModel;
import com.bona.model.system.SysConfigModel;
import com.bona.model.system.SysDataSourceModel;
import com.bona.model.system.SysLogModel;
import com.bona.model.system.SysMultLangModel;
import com.bona.model.system.SysOperationModel;
import com.bona.model.system.SysRoleFunctionModel;
import com.bona.model.system.SysSmsModel;
import com.bona.model.system.SysTerritoryModel;
import com.bona.model.system.SysTypeGroupModel;
import com.bona.model.system.SysTypeModel;
import com.bona.service.system.SysCategoryService;
import com.bona.service.system.SysCommonService;
import com.bona.service.system.SysDataSourceService;
import com.bona.service.system.SysMultLangService;
import com.bona.service.system.SysOperationService;
import com.bona.service.system.SysRoleService;
import com.bona.service.system.SysTerritoryService;
import com.bona.service.system.SysTypeGroupService;
import com.bona.service.system.SysTypeService;
import com.bona.util.common.UUIDGenerator;

/**
 * 系统常用方法对外接口
 * @author tbzeng
 *
 */
public class SysCommonDubboSVImpl implements ISysCommonDubboSV {

	@Autowired
	private SysCommonService commonService;
	@Autowired
	private SysRoleService roleService;
	@Autowired
	private SysOperationService operationService;
	@Autowired
	private SysTypeService typeService;
	@Autowired
	private SysTypeGroupService typeGroupService;
	@Autowired
	private SysMultLangService multLangService;
	@Autowired
	private SysDataSourceService dataSourceService;
	@Autowired
	private SysTerritoryService territoryService;
	@Autowired
	private SysCategoryService categoryService;
	@Autowired
	private SysMultLangCache multLangCache;
	
	@Override
	public Integer addLog(SysLogModel model) {
		return commonService.addLog(model);
	}
	
	@Override
	public Integer addLog(String logContent, Short operateType, Short logLevel, String userId) {
		
		SysLogModel model = new SysLogModel();
		model.setId(UUIDGenerator.generate());
		model.setLogcontent(logContent);
		model.setOperatetype(operateType);
		model.setLoglevel(logLevel);
		model.setUserid(userId);
		model.setOperatetime(new Date());
		return commonService.addLog(model);
	}

	@Override
	public String getLang(String langKey) {
		
		if (StringUtils.isEmpty(langKey)) return "";
		String langContext = multLangCache.getMapValue(langKey+CacheConstant.SPLIT_CHAR+"zh-cn");
		return langContext==null? langKey: langContext;
	}

	@Override
	public String getLang(String langKey, String langArg) {
		
		if (StringUtils.isEmpty(langKey)) return "";
		String langContext = multLangCache.getMapValue(langKey+CacheConstant.SPLIT_CHAR+"zh-cn");
		String context = langContext==null? langKey: langContext;
		if (StringUtils.isEmpty(langArg)) return context; 
		String[] langArgArr = langArg.split(",");
		for (int idx=0; idx<langArgArr.length; idx++) {
			String langArgContext = multLangCache.getMapValue(langArgArr[idx]+CacheConstant.SPLIT_CHAR+"zh-cn");
			langArgContext = langArgContext==null? "": langArgContext;
			context = context.replaceAll("\\{"+idx+"\\}", langArgContext);
		}
		return context;
	}

	@Override
	public List<Map<String, String>> queryDic(String dictField, String dictText, String dictTable) {
		List<Map<String, String>> dictList = commonService.queryDict(dictField, dictText, dictTable);
		return dictList;
	}

	@Override
	public List<SysConfigModel> queryConfig() {
		return commonService.queryConfig();
	}

	@Override
	public List<SysSmsModel> querySMs(SysSmsModel model) {
		return commonService.querySMs(model);
	}

	@Override
	public SysOperationModel queryOptByCode(String operationCode) {
		return commonService.queryOptByCode(operationCode);
	}
	
	@Override
	public List<SysRoleFunctionModel> queryRoleFunctionByFuncId(String funcId) {
		return roleService.queryRoleFunctionByFuncId(funcId);
	}
	
	@Override
	public Integer queryEditDumplicate(String tableName, String fieldName, String fieldValue, String rowId) {
		return commonService.queryEditDumplicate(tableName, fieldName, fieldValue, rowId);
	}

	@Override
	public List<SysOperationModel> queryOptByFuncId(String functionId) {
		SysOperationModel model = new SysOperationModel();
		model.setFunctionid(functionId);
		List<SysOperationModel> models = operationService.queryByPage(model);
		return models;
	}
	
	@Override
	public SysTypeModel queryType(SysTypeModel model) {
		return typeService.query(model);
	}
	
	@Override
	public void addOrUpdateType(SysTypeModel model) {
		typeService.addOrUpdate(model);
	}
	
	@Override
	public Integer deleteType(SysTypeModel model) {
		return typeService.delete(model);
	}
	
	@Override
	public Integer queryTypeByCount(SysTypeModel model) {
		return typeService.queryByCount(model);
	}
	
	@Override
	public List<SysTypeModel> queryTypeByPage(SysTypeModel model) {
		return typeService.queryByPage(model);
	}
	
	@Override
	public SysTypeGroupModel queryTypeGroup(SysTypeGroupModel model) {
		return typeGroupService.query(model);
	}
	
	@Override
	public void deleteTypeGroup(SysTypeGroupModel model) {
		typeGroupService.delete(model);
	}
	
	@Override
	public void addOrUpdateTypeGroup(SysTypeGroupModel model) {
		typeGroupService.addOrUpdateTypeGroup(model);
	}
	
	@Override
	public Integer queryTypeGroupByCount(SysTypeGroupModel model) {
		return typeGroupService.queryByCount(model);
	}
	
	@Override
	public List<SysTypeGroupModel> queryTypeGroupByPage(SysTypeGroupModel model) {
		return typeGroupService.queryByPage(model);
	}
	
	@Override
	public SysMultLangModel queryMultLang(SysMultLangModel model) {
		return multLangService.query(model);
	}
	
	@Override
	public void addOrUpdateMultLang(SysMultLangModel model) {
		multLangService.addOrUpdateMultLang(model);
	}
	
	@Override
	public Integer deleteMultLang(SysMultLangModel model) {
		return multLangService.delete(model);
	}
	
	@Override
	public Integer queryMultLangByCount(SysMultLangModel model) {
		return multLangService.queryByCount(model);
	}
	
	@Override
	public List<SysMultLangModel> queryMultLangByPage(SysMultLangModel model) {
		return multLangService.queryByPage(model);
	}
	
	@Override
	public SysDataSourceModel queryDataSource(SysDataSourceModel model) {
		return dataSourceService.query(model);
	}
	
	@Override
	public void addOrUpdateDataSource(SysDataSourceModel model) {
		dataSourceService.addOrUpdateSysDataSource(model);
	}
	
	@Override
	public Integer deleteDataSource(SysDataSourceModel model) {
		return dataSourceService.delete(model);
	}
	
	@Override
	public Integer queryDataSourceByCount(SysDataSourceModel model) {
		return dataSourceService.queryByCount(model);
	}
	
	@Override
	public List<SysDataSourceModel> queryDataSourceByPage(SysDataSourceModel model) {
		return dataSourceService.queryByPage(model);
	}
	
	@Override
	public SysTerritoryModel queryTerritory(SysTerritoryModel model) {
		return territoryService.query(model);
	}
	
	@Override
	public void addOrUpdateTerritory(SysTerritoryModel model) {
		territoryService.addOrTerritory(model);
	}
	
	@Override
	public Integer deleteTerritory(SysTerritoryModel model) {
		return territoryService.delete(model);
	}
	
	@Override
	public Integer queryTerritoryByCount(SysTerritoryModel model) {
		return territoryService.queryByCount(model);
	}
	
	@Override
	public List<SysTerritoryModel> queryTerritoryByPage(SysTerritoryModel model) {
		return territoryService.queryByPage(model);
	}
	
	@Override
	public List<SysTerritoryModel> queryTerritoryByPageRecursive(SysTerritoryModel model) {
		List<SysTerritoryModel> territoryList = territoryService.queryByPage(model);
		for (SysTerritoryModel terri1: territoryList) {
			model = new SysTerritoryModel();
			model.setTerritoryparentid(terri1.getId());
			List<SysTerritoryModel> childTerritory1 = territoryService.queryByPage(model);
			terri1.setChildTerritory(childTerritory1);
		}
		return territoryList;
	}
	
	@Override
	public SysCategoryModel queryCategory(SysCategoryModel model) {
		return categoryService.query(model);
	}
	
	@Override
	public void addOrUpdateCategory(SysCategoryModel model) {
		categoryService.addOrUpdateCategory(model);
	}
	
	@Override
	public void deleteCategory(SysCategoryModel model) {
		categoryService.delete(model);
	}
	
	@Override
	public Integer queryCategoryByCount(SysCategoryModel model) {
		return categoryService.queryByCount(model);
	}
	
	@Override 
	public List<SysCategoryModel> queryCategoryByPage(SysCategoryModel model) {
		return categoryService.queryByPage(model);
	}
}
